package com.sino.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sino.bean.TModule;
import com.sino.dao.ITModuleDAO;

public class TModuleDAO extends HibernateDaoSupport implements ITModuleDAO {
	private static final Log log = LogFactory.getLog(TModuleDAO.class);

	public void delTModule(int id) {
		this.getHibernateTemplate().delete(this.getTModule(id));

	}

	public void saveTModule(TModule tmodule) {
		this.getHibernateTemplate().save(tmodule);

	}

	public void updateTModule(TModule tmodule) {
		this.getHibernateTemplate().update(tmodule);

	}

	public TModule getTModule(int id) {
		return (TModule) this.getHibernateTemplate().get(TModule.class, id);
	}

	public int findPageCount(String hqlwhere) {

		try {
			return (Integer) this.getSession().createQuery(hqlwhere)
					.uniqueResult();

		} catch (RuntimeException re) {
			log.error("findmodulePageCount", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TModule> findModulePageList(final int firstRow,
			final int maxRow, String hqlwhere) {

		final String queryhql = hqlwhere;

		return (List<TModule>) this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(queryhql);
						query.setFirstResult(firstRow);
						query.setMaxResults(maxRow);
						List<TModule> list = (List<TModule>) query.list();
						return list;
					}
				});
	}
}
